package com.yic.module.device.service.resume;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import com.yic.module.device.controller.admin.resume.vo.*;
import com.yic.module.device.dal.dataobject.resume.ResumeDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.device.convert.resume.ResumeConvert;
import com.yic.module.device.dal.mysql.resume.ResumeMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.device.enums.ErrorCodeConstants.*;

/**
 * 设备履历 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ResumeServiceImpl implements ResumeService {

    @Resource
    private ResumeMapper resumeMapper;

    @Override
    public Long createResume(ResumeCreateReqVO createReqVO) {
        // 插入
        ResumeDO resume = ResumeConvert.INSTANCE.convert(createReqVO);
        resumeMapper.insert(resume);
        // 返回
        return resume.getId();
    }

    @Override
    public Long createResume(Long deviceId, Integer type, String description, Long sourceId, Long operateUser, LocalDateTime operateTime) {
        ResumeCreateReqVO createReqVO = new ResumeCreateReqVO();
        createReqVO.setDeviceId(deviceId);
        createReqVO.setType(type);
        createReqVO.setDescription(description);
        createReqVO.setSourceId(sourceId);
        createReqVO.setOperateUser(operateUser);
        createReqVO.setOperateTime(operateTime);
        return createResume(createReqVO);
    }

    @Override
    public void updateResume(ResumeUpdateReqVO updateReqVO) {
        // 校验存在
        validateResumeExists(updateReqVO.getId());
        // 更新
        ResumeDO updateObj = ResumeConvert.INSTANCE.convert(updateReqVO);
        resumeMapper.updateById(updateObj);
    }

    @Override
    public void deleteResume(Long id) {
        // 校验存在
        validateResumeExists(id);
        // 删除
        resumeMapper.deleteById(id);
    }

    private void validateResumeExists(Long id) {
        if (resumeMapper.selectById(id) == null) {
            throw exception(RESUME_NOT_EXISTS);
        }
    }

    @Override
    public ResumeDO getResume(Long id) {
        return resumeMapper.selectById(id);
    }

    @Override
    public List<ResumeDO> getResumeList(Collection<Long> ids) {
        return resumeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ResumeDO> getResumePage(ResumePageReqVO pageReqVO) {
        return resumeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResumeDO> getResumeList(ResumeExportReqVO exportReqVO) {
        return resumeMapper.selectList(exportReqVO);
    }

    @Override
    public void deleteResumeBySourceId(Long id, Integer deviceResumeType) {
        resumeMapper.deleteBySourceId(id, deviceResumeType);
    }

}
